//master_invited
//邀请回答—达人
exports.master_invited = function (req, res) {

    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var ficid = 0;

    var fic_type = 0;

    var inviteLog = 0;

    var inviteCount = 0;

    var organization = 0;

    //任务流创建
    async.waterfall([

            //1.判断这个问题是否被邀请过
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchForumInviteContent + "/fic_invited_type/" + restfulParams.invited_type + "/fic_cid/" + restfulParams.fc_id,
                        method: 'POST'
                    },
                    function (err, data) {
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }

                        if (data.data.length > 0) {

                            ficid = data.data[0].fic_id;
                            fic_type = data.data[0].fic_invited_type;
                            cb();

                            //新增问题记录
                        } else {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.addForumInviteContent,
                                    method: 'POST',
                                    data: {
                                        fic_cid: restfulParams.fc_id,
                                        fic_invited_type: restfulParams.invited_type,
                                        fic_buid: restfulParams.m_uid
                                    }
                                }
                                ,
                                function (err, data) {

                                    //判断是否有错误信息
                                    if (err) {
                                        return RES.response(res, false, "邀请回答失败33");
                                    }

                                    ficid = data;
                                    cb();

                                }
                            );

                        }

                    }
                );

            },
            //查询当前用户是否邀请过该达人
            function (cb) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchForumInviteLog + "/fil_invite_type/" + restfulParams.invited_type + "/fil_uid/" + restfulParams.u_id + "/fil_ficid/" + ficid + "/fil_fid/" + restfulParams.m_uid,
                        method: 'GET'
                    }
                    ,
                    function (err, data) {

                        //判断是否有错误信息
                        if (err) {
                            // return RES.response(res, false, "邀请回答失败");
                            return RES.response(res, false, data);
                        }

                        inviteLog = data.data;

                        cb();

                    }
                );

            },

            function (cb) {
                if (inviteLog.length > 0) {
                    return RES.response(res, false, "您已邀请过该达人或机构了");
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.addForumInviteLog,
                            method: 'POST',
                            data: {
                                fil_uid: restfulParams.u_id,
                                fil_ficid: ficid,
                                fil_fcid: restfulParams.fc_id,
                                fil_invite_type: restfulParams.invited_type,
                                fil_fid: restfulParams.m_uid
                            }
                        },
                        function (err, data) {

                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, "邀请回答失败11");
                            }

                            cb();

                        }
                    );
                }

            },
            //查询该达人被邀请回答的次数
            function (cb) {
                if (restfulParams.invited_type == 1) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchMasterInfo + "/m_uid/" + restfulParams.m_uid,
                            method: 'POST',
                            data: {
                                fields: ["m_uid", "m_invitation"].join(",")
                            }
                        },
                        function (err, data) {

                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, "邀请回答失败22");
                            }
                            inviteCount = data.data[0].m_invitation;

                            cb();

                        }
                    );
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchOrganization + "/o_uid/" + restfulParams.m_uid,
                            method: 'POST',
                            data: {
                                fields: ["o_id", "o_invite_count"].join(",")
                            }
                        },
                        function (err, data) {

                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, "邀请回答失败22");
                            }

                            inviteCount = data.data[0].o_invite_count;

                            organization = data.data[0]

                            cb();

                        }
                    );
                }

            },
            //更新达人被邀请回答的次数
            function (cb) {
                if (restfulParams.invited_type == 1) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateMaster,
                            method: 'POST',
                            data: {
                                m_uid: restfulParams.m_uid,
                                m_invitation: parseInt(inviteCount) + 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data;
                            cb();
                        }
                    );
                } else {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.updateOrganization,
                            method: 'POST',
                            data: {
                                o_id: organization.o_id,
                                o_invite_count: parseInt(inviteCount) + 1
                            }
                        },
                        function (err, data) {
                            //判断是否有错误信息
                            if (err) {
                                return RES.response(res, false, data);
                            }

                            resultData.updateData = data;
                            cb();
                        }
                    );
                }
            },
        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true);

        }
    );

}